<?php

function CompatibleListFolderData($mes) {
  global $_GET, $path, $style, $session_id, $data, $term_Thing_ID, $language, $header;
  $filename = $path[$style]["tpl"]."/attrdata.xml";
  $fp = fopen($filename, "r");
  $contents = fread($fp, filesize ($filename));
  fclose ($fp);

  preg_match("/\%REPEAT\%(.*?)\%REPEAT\%/s", $contents, $matches);
  preg_match("/\%REPDAT\%(.*?)\%REPDAT\%/s", $matches[1], $datmatches);

  $udata = str_replace($datmatches[0], "", $matches[1]);
  $udata = str_replace("%GROUP_ID%", "0", $udata);
  $udata = str_replace("%GROUP_TL%", $mes, $udata);
  $contents = str_replace("%DatHref%", "index.php?trg=ontoedit&amp;act=compatiblelistdata&amp;sid=".
      $session_id."&amp;uid=".$_GET["uid"], $contents);

  header ($header["xml"]);
  return str_replace($matches[0], $udata, $contents);
}

function getCompatible($class_id, $matches, $session_id){
  global $_GET, $undef_id, $language;
  $next_class_id = $class_id;
  $parent_class_id = $next_class_id;
  $sql_str = "SELECT t.text FROM class c, term t WHERE t.id=c.term_id and c.id=".$class_id;
  getData($sql_str, $next_data);
  $inherited = false;
  $rep_cont = "";
  while ($next_data["text"] != "Thing"){
    $sql_str = "SELECT * FROM compatible com, term t, class c WHERE com.class2_id=c.id and c.term_id=t.id and t.lang_id = ".$language["content"]." and com.class1_id=".$next_class_id;
    $i_max = getMultData($sql_str, $ontosdata);

    $sql_str = "SELECT * FROM compatible c, term t WHERE t.id=".$undef_id." and t.lang_id = ".$language["content"]." and c.class2_id=-1 and c.class1_id=".$next_class_id;
    if (getData($sql_str, $ontosdata1) == 1){
      $ontosdata[$i_max]["class2_id"] = $ontosdata1["class2_id"];
      $ontosdata[$i_max]["text"] = $ontosdata1["text"];
      $i_max++;
    };

    for ($i = 0; $i < $i_max; $i++) {
      $tmp_attr = str_replace("%CLASS_ID%", "com".$parent_class_id."com".$ontosdata[$i]["class2_id"], $matches[1]);

      if (isset($_GET["sel"]) && $_GET["sel"] == "com".$parent_class_id."com".$ontosdata[$i]["class2_id"])
        $tmp_attr = str_replace("%ACT%", "select", $tmp_attr);

      $tmp_attr = str_replace("%CLASS_NM%", $ontosdata[$i]["text"], $tmp_attr);
      if ($inherited)
        $tmp_attr = str_replace("%IMG_TYPE%", "inhattr", $tmp_attr);
      else
        $tmp_attr = str_replace("%IMG_TYPE%", "attr", $tmp_attr);
      $tmp_attr = str_replace("%CLASS_LN%", "index.php?trg=ontoedit&amp;act=compatibledata&amp;sid=".
      $session_id."&amp;uid="."com".$parent_class_id."com".$ontosdata[$i]["class2_id"], $tmp_attr);
      $tmp_attr = str_replace("%CHILD_LN%", "", $tmp_attr);
      if (isset($_GET["sel"]) &&
        ($_GET["sel"] == "com".$parent_class_id."com".$ontosdata[$i]["class2_id"] ||
        ($_GET["sel"] == 0 && $i == 0))) {
        $tmp_attr = str_replace("%ACT%", "select", $tmp_attr);
      }
      $rep_cont .= $tmp_attr;
    }

    $sql_str = "SELECT * FROM compatible com, term t, class c WHERE com.class1_id=c.id and c.term_id=t.id and t.lang_id = ".$language["content"]." and com.class2_id=".$next_class_id;
    $i_max = getMultData($sql_str, $ontosdata);

    $sql_str = "SELECT * FROM compatible c, term t WHERE t.id=".$undef_id." and t.lang_id = ".$language["content"]." and c.class2_id=-1 and c.class2_id=".$next_class_id;
    if (getData($sql_str, $ontosdata1) == 1){
      $ontosdata[$i_max]["class1_id"] = $ontosdata1["class1_id"];
      $ontosdata[$i_max]["text"] = $ontosdata1["text"];
      $i_max++;
    };

    for ($i = 0; $i < $i_max; $i++) {
      $tmp_attr = str_replace("%CLASS_ID%", "com".$parent_class_id."com".$ontosdata[$i]["class1_id"], $matches[1]);

      if (isset($_GET["sel"]) && $_GET["sel"] == "com".$parent_class_id."com".$ontosdata[$i]["class1_id"])
        $tmp_attr = str_replace("%ACT%", "select", $tmp_attr);

      $tmp_attr = str_replace("%CLASS_NM%", $ontosdata[$i]["text"], $tmp_attr);
      if ($inherited)
        $tmp_attr = str_replace("%IMG_TYPE%", "inhattr", $tmp_attr);
      else
        $tmp_attr = str_replace("%IMG_TYPE%", "attr", $tmp_attr);
      $tmp_attr = str_replace("%CLASS_LN%", "index.php?trg=ontoedit&amp;act=compatibledata&amp;sid=".
      $session_id."&amp;uid="."com".$parent_class_id."com".$ontosdata[$i]["class1_id"], $tmp_attr);
      $tmp_attr = str_replace("%CHILD_LN%", "", $tmp_attr);
      $rep_cont .= $tmp_attr;
    }

    $sql_str = "SELECT parent_id FROM class WHERE id=".$next_class_id;
    getData($sql_str, $next_data);
    $next_class_id = $next_data["parent_id"];
    $sql_str = "SELECT t.text FROM class c, term t WHERE t.id=c.term_id and c.id=".$next_class_id;
    getData($sql_str, $next_data);
    $inherited = true;
  };
  return $rep_cont;
};


function compatibleList(){
 global $_GET, $data, $path, $style, $session_id, $language, $header;
  $filename = $path[$style]["tpl"]."/classes.xml";
  $fp = fopen($filename, "r");
  $contents = fread($fp, filesize ($filename));
  fclose ($fp);

  preg_match("/\%REPEAT\%(.*?)\%REPEAT\%/s", $contents, $matches);
  $rep_cont = getCompatible(str_replace("com","",$_GET["uid"]), $matches, $session_id);

  $contents = str_replace($matches[0], $rep_cont, $contents);
  header ($header["xml"]);
  return $contents;
};

function my_com_sort($childs){
  for ($i = 0; $i < count($childs) - 1; $i++){
    for ($j = 0; $j < count($childs) - 1; $j++){
      if (strcmp($childs[$j]["text"], $childs[$j + 1]["text"]) > 0){
        $time = $childs[$j]["text"];
        $childs[$j]["text"] = $childs[$j + 1]["text"];
        $childs[$j + 1]["text"] = $time;

        $time = $childs[$j]["id"];
        $childs[$j]["id"] = $childs[$j + 1]["id"];
        $childs[$j + 1]["id"] = $time;
      };
    };
  };
  return $childs;
};

function get_com_child($parent, $bad_child, &$childs, &$count_childs, $ID){
  global $language;
  $i_max = getMultData("SELECT t.text, c.id FROM class c, term t ".
              "WHERE c.term_id=t.id and t.lang_id = ".$language["content"]." and c.parent_id=".$parent." and c.id<>".$bad_child, $related_class);
  for ($i = 0; $i < $i_max; $i++){
      $childs[$count_childs]["id"] = $related_class[$i]["id"];
      $childs[$count_childs]["text"] = $related_class[$i]["text"];
      $count_childs++;
    get_com_child($related_class[$i]["id"], 0, $childs, $count_childs, $ID);
  };
};

function get_com_related_class(&$childs, $ID){
  getData("SELECT parent_id FROM class ".
          "WHERE id=".$ID, $related_class);
  $childs = Array();$count_childs = 0;
  if ($ID != "Thing")
    get_com_child($related_class["parent_id"], $ID, $childs, $count_childs, $ID);

  getData("SELECT t.text FROM class c, term t ".
          "WHERE c.term_id=t.id and c.id=".$related_class["parent_id"], $name_of_class);
  while ($name_of_class["text"] != "Thing"){
    $bad_class = $related_class["parent_id"];
    getData("SELECT c.parent_id, t.text FROM class c, term t ".
            "WHERE c.term_id=t.id and c.id=".$related_class["parent_id"], $related_class);
    get_com_child($related_class["parent_id"], $bad_class, $childs, $count_childs, $ID);
    getData("SELECT t.text FROM class c, term t ".
            "WHERE c.term_id=t.id and c.id=".$related_class["parent_id"], $name_of_class);
  };

  $childs = my_com_sort($childs);
};

function compatibleData($new_id) {
  global $_GET, $path, $style, $session_id, $data, $term_Thing_ID, $language, $header;
  $filename = $path[$style]["tpl"]."/attrdata.xml";
  $fp = fopen($filename, "r");
  $contents = fread($fp, filesize ($filename));
  fclose ($fp);

  preg_match("/\%REPEAT\%(.*?)\%REPEAT\%/s", $contents, $matches);
  preg_match("/\%REPDAT\%(.*?)\%REPDAT\%/s", $matches[1], $datmatches);
  preg_match("/\%REPOPT\%(.*?)\%REPOPT\%/s", $datmatches[1], $optmatches);
  $rep_cont = "";

  $ID[0] = substr($_GET["uid"], strpos($_GET["uid"], "com") + 3,strrpos($_GET["uid"], "com") - strpos($_GET["uid"], "com") - 3);
  $ID[1] = substr($_GET["uid"], strrpos($_GET["uid"], "com") + 3);

  if ($new_id > -1){
    $ID[1] = $new_id;
    $_GET["uid"] = "com".$ID[0]."com".$ID[1];
  };

  getData("SELECT * FROM term t, class c ".
          "WHERE c.term_id=t.id and c.id=".$ID[0], $class_name);
  $class_name["text"] = htmlspecialchars($class_name["text"]);

  getData("SELECT * FROM term t, class c ".
          "WHERE c.term_id=t.id and c.id=".$ID[1], $class_name1);
  $class_name1["text"] = htmlspecialchars($class_name1["text"]);

  getData("SELECT * FROM term t, class c, ontology o ".
          "WHERE c.onto_id=o.id and o.term_id=t.id and c.id=".$ID[0], $onto_name);
  $onto_name["text"] = htmlspecialchars($onto_name["text"]);

  getData("SELECT is_compatible FROM compatible ".
          "WHERE (class1_id=".$ID[0]." and class2_id=".$ID[1].") or (class2_id=".$ID[0]." and class1_id=".$ID[1].")", $is_compatible);

  $childs1 = Array();$childs = Array();
  if ($ID[1] == -1){
    get_com_related_class($childs1, $ID[0]);

    $childs_com = Array(); $count = 0;
    com_child($ID[0], $childs_com, $count);
    $k = 0;
    //$ID[3] = $ID[0];
    for ($i = 0; $i < count($childs1); $i++){
      $b = true;
      for ($j = 0; $j < count($childs_com); $j++){
        if ($childs1[$i]["id"] == $childs_com[$j]["id"])
          $b = false;
      };
      if ($b){
          $childs[$k] = $childs1[$i];
          $k++;
        };
    };
    $childs1 = $childs;

    $inh_com = Array(); $childs = Array();
    $count = 0; $k = 0;
    get_com_inherited($ID[0], $inh_com, $count);

    //Compatible / Incompatible
    for ($i = 0; $i < count($inh_com); $i++)
      if ($ID[1] == $inh_com[$i]["id"])
        $ID[3] = $inh_com[$i]["class_id"];
    //End of Compatible / Incompatible

    for ($i = 0; $i < count($childs1); $i++){
      $b = true;
      for ($j = 0; $j < count($inh_com); $j++){
        if ($childs1[$i]["id"] == $inh_com[$j]["id"])
          $b = false;
      };
      if ($b){
          $childs[$k] = $childs1[$i];
          $k++;
        };

    };
    if (count($inh_com) == 0)
      $childs = $childs1;

    //Test if acon or hierarchy
    $childs1 = $childs;
    include "acon.php";
    include "hierarchy.php";
    $inh_acon = Array(); $childs = Array();
    $count = 0; $k = 0;
    get_acon_inherited($ID[0], $inh_acon, $count);
    $inh_h = Array();
    $count = 0;
    get_h_inherited($ID[0], $inh_h, $count);
    for ($i = 0; $i < count($childs1); $i++){
      $b = true;
      for ($j = 0; $j < count($inh_acon); $j++){
        if ($childs1[$i]["id"] == $inh_acon[$j])
          $b = false;
      };
      for ($j = 0; $j < count($inh_h); $j++){
        if ($childs1[$i]["id"] == $inh_h[$j])
          $b = false;
      };
      if ($b){
          $childs[$k] = $childs1[$i];
          $k++;
        };
    };
  };
  $udata = "";
  if ($data["type"] >= 1) { //user
    //New name
    if ($new_id > -1){
      $contents = str_replace("%NAME%", $class_name1["text"], $contents);
      $contents = str_replace("%ID%", $_GET["uid"], $contents);
      $contents = str_replace("%DatHref%", "index.php?trg=ontoedit&amp;act=compatibledata&amp;sid=".
        $session_id."&amp;uid=".$_GET["uid"], $contents);
    };

    //Class Name
    $classlb = str_replace($optmatches[0], "", $datmatches[1]);
    $classlb = str_replace("%TYPE%", "Label", $classlb);
    $classlb = str_replace("%NODE_ID%", "cname", $classlb);
    $classlb = str_replace("%NODE_TL%", getMessage("Class Name"), $classlb);
    $classlb = str_replace("%VALUE%", $class_name["text"], $classlb);
    //Ontology Name
    $iclasslb = str_replace($optmatches[0], "", $datmatches[1]);
    $iclasslb = str_replace("%TYPE%", "Label", $iclasslb);
    $iclasslb = str_replace("%NODE_ID%", "oname", $iclasslb);
    $iclasslb = str_replace("%NODE_TL%", getMessage("Ontology Name"), $iclasslb);
    $iclasslb = str_replace("%VALUE%", $onto_name["text"], $iclasslb);

    $iclass1lb = str_replace($optmatches[0], "", $datmatches[1]);
    $iclass1lb = str_replace("%TYPE%", "Hidden", $iclass1lb);
    $iclass1lb = str_replace("%NODE_ID%", "ohidd", $iclass1lb);
    $iclass1lb = str_replace("%NODE_TL%", "", $iclass1lb);
    $iclass1lb = str_replace("%VALUE%", $ID[0], $iclass1lb);

    $iclass2lb = str_replace($optmatches[0], "", $datmatches[1]);
    $iclass2lb = str_replace("%TYPE%", "Hidden", $iclass2lb);
    $iclass2lb = str_replace("%NODE_ID%", "ohidd2", $iclass2lb);
    $iclass2lb = str_replace("%NODE_TL%", "", $iclass2lb);
    $iclass2lb = str_replace("%VALUE%", $ID[1], $iclass2lb);

    //echo $ID[0]."!!!".$ID[1];
    $count = 0; $inh_com = Array();
    getData("SELECT parent_id FROM class WHERE id=".$ID[0], $parentdata);
    getData("SELECT * FROM class c, term t WHERE t.id=c.term_id and c.id=".$parentdata["parent_id"], $parentdata1);
    if ($parentdata1["text"] != "Thing")
      get_com_inherited($parentdata["parent_id"], $inh_com, $count);

    $b = true;
    for ($i = 0; $i < count($inh_com); $i++)
      if ($inh_com[$i]["id"] == $ID[1]){
        $inh_class_id = $inh_com[0]["class_id"];
        $b = false;
      };
    if ($b){
        if ((getdata("SELECT * FROM compatible WHERE (class1_id=".$ID[0]." and class2_id=".$ID[1].") or (class2_id=".$ID[0]." and class1_id=".$ID[1].")", $onto_data) == 1) && (count($childs) > 0)){
          $opts = "";
          for ($i = 0; $i < count($childs); $i++) {
            $childs[$i]["text"] = htmlspecialchars($childs[$i]["text"]);
            $opt = str_replace("%OPT_ID%", $childs[$i]["id"], $optmatches[1]);
            $opt = str_replace("%OPT_TL%", $childs[$i]["text"], $opt);
            if ($childs[$i]["id"] == $ID[1])
              $opt = str_replace("%SELECTED%", "selected", $opt);
            else
              $opt = str_replace("%SELECTED%", "0", $opt);
            $opts .= $opt;
          }

          $opt1 = str_replace("%OPT_ID%", "1", $optmatches[1]);
          $opt1 = str_replace("%OPT_TL%", getMessage("yes"), $opt1);
          if (($is_compatible["is_compatible"] == "1")||($is_compatible["is_compatible"] == ""))
            $opt1 = str_replace("%SELECTED%", "selected", $opt1);
          else
            $opt1 = str_replace("%SELECTED%", "0", $opt1);
          $opts1 = $opt1;
          $opt1 = str_replace("%OPT_ID%", "0", $optmatches[1]);
          $opt1 = str_replace("%OPT_TL%", getMessage("no"), $opt1);
          if ($is_compatible["is_compatible"] == "0")
            $opt1 = str_replace("%SELECTED%", "selected", $opt1);
          else
            $opt1 = str_replace("%SELECTED%", "0", $opt1);
          $opts1 .= $opt1;

          //domain
          $utype = str_replace($optmatches[0], $opts, $datmatches[1]);
          $utype = str_replace("%TYPE%", "Option", $utype);
          $utype = str_replace("%NODE_ID%", "otype", $utype);
          $utype = str_replace("%NODE_TL%", getMessage("Related Class"), $utype);

          $utype1 = str_replace($optmatches[0], $opts1, $datmatches[1]);
          $utype1 = str_replace("%TYPE%", "Option", $utype1);
          $utype1 = str_replace("%NODE_ID%", "otype1", $utype1);
          $utype1 = str_replace("%NODE_TL%", getMessage("Compatibility / Incompatibility"), $utype1);

          $ubutn = str_replace($optmatches[0], "", $datmatches[1]);
          $ubutn = str_replace("%TYPE%", "Button", $ubutn);
          $ubutn = str_replace("%NODE_ID%", "obutn", $ubutn);
          $ubutn = str_replace("%NODE_TL%", getMessage("Save"), $ubutn);
          $ubutn = str_replace("%VALUE%", "'0','otype', 'otype1', 'ohidd', 'ohidd2'", $ubutn);
        }else{
          $opt1 = str_replace("%OPT_ID%", "1", $optmatches[1]);
          $opt1 = str_replace("%OPT_TL%", getMessage("yes"), $opt1);
          if (($is_compatible["is_compatible"] == "1")||($is_compatible["is_compatible"] == ""))
            $opt1 = str_replace("%SELECTED%", "selected", $opt1);
          else
            $opt1 = str_replace("%SELECTED%", "0", $opt1);
          $opts1 = $opt1;
          $opt1 = str_replace("%OPT_ID%", "0", $optmatches[1]);
          $opt1 = str_replace("%OPT_TL%", getMessage("no"), $opt1);
          if ($is_compatible["is_compatible"] == "0")
            $opt1 = str_replace("%SELECTED%", "selected", $opt1);
          else
            $opt1 = str_replace("%SELECTED%", "0", $opt1);
          $opts1 .= $opt1;

          $utype1 = str_replace($optmatches[0], $opts1, $datmatches[1]);
          $utype1 = str_replace("%TYPE%", "Option", $utype1);
          $utype1 = str_replace("%NODE_ID%", "otype1", $utype1);
          $utype1 = str_replace("%NODE_TL%", getMessage("Compatibility / Incompatibility"), $utype1);

          $ubutn = str_replace($optmatches[0], "", $datmatches[1]);
          $ubutn = str_replace("%TYPE%", "Button", $ubutn);
          $ubutn = str_replace("%NODE_ID%", "obutn", $ubutn);
          $ubutn = str_replace("%NODE_TL%", getMessage("Save"), $ubutn);
          $ubutn = str_replace("%VALUE%", "'0', 'otype1', 'ohidd', 'ohidd2'", $ubutn);

          $utype = "";
        };
    }else{
        $utype1 = str_replace($optmatches[0], "", $datmatches[1]);
        $utype1 = str_replace("%TYPE%", "Label", $utype1);
        $utype1 = str_replace("%NODE_ID%", "otype1", $utype1);
        $utype1 = str_replace("%NODE_TL%", getMessage("Compatibility / Incompatibility"), $utype1);

        getData($sql = "SELECT is_compatible FROM compatible ".
                "WHERE (class1_id=".$inh_class_id." and class2_id=".$ID[1].") or (class2_id=".$inh_class_id." and class1_id=".$ID[1].")", $is_compatible);

        if ($is_compatible["is_compatible"] == "0")
          $utype1 = str_replace("%VALUE%", getMessage("no"), $utype1);
        else
          $utype1 = str_replace("%VALUE%", getMessage("yes"), $utype1);


        $ubutn = "";
        $utype = "";
    };

    $udata = str_replace($datmatches[0], $classlb.$iclasslb.$utype.$utype1.$ubutn.$iclass1lb.$iclass2lb, $matches[1]);
    $udata = str_replace("%GROUP_ID%", "0", $udata);
    $udata = str_replace("%GROUP_TL%", getMessage("Compatibility"), $udata);

//Action
    $btns = "";
    if ($new_id == -1){
      $btn1 = str_replace($optmatches[0], "", $datmatches[1]);
      $btn1 = str_replace("%TYPE%", "Button", $btn1);
      $btn1 = str_replace("%NODE_ID%", "", $btn1);
      $btn1 = str_replace("%NODE_TL%", getMessage("Ontologies"), $btn1);
      $btn1 = str_replace("%VALUE%", "'wd_tree', 'index.php?trg=ontoedit&amp;sid=".
        $session_id."&amp;act=ontolist&amp;sel=".$onto_name["id"]."'", $btn1);

      $btns = str_replace($datmatches[0], $btn1, $matches[1]);
      $btns = str_replace("%GROUP_ID%", "1", $btns);
      $btns = str_replace("%GROUP_TL%", getMessage("Action"), $btns);
    }
  }
  else { //guest
/*    //New name
    $contents = str_replace("%NAME%", $ontodata["name"], $contents);
    //Name
    $fname = str_replace($optmatches[0], "", $datmatches[1]);
    $fname = str_replace("%TYPE%", "Label", $fname);
    $fname = str_replace("%NODE_ID%", "oname", $fname);
    $fname = str_replace("%NODE_TL%", getMessage("Name"), $fname);
    $fname = str_replace("%VALUE%", $ontodata["name"], $fname);
    //Description
    $lname = str_replace($optmatches[0], "", $datmatches[1]);
    $lname = str_replace("%TYPE%", "Label", $lname);
    $lname = str_replace("%NODE_ID%", "odescr", $lname);
    $lname = str_replace("%NODE_TL%", getMessage("Description"), $lname);
    $lname = str_replace("%VALUE%", $ontodata["description"], $lname);
    //User
    $udata = str_replace($datmatches[0], $fname.$lname, $matches[1]);
    $udata = str_replace("%GROUP_ID%", "0", $udata);
    $udata = str_replace("%GROUP_TL%", getMessage("Attribute data"), $udata);
    //print "****".$udata."****";*/
  }
  header ($header["xml"]);
  return str_replace($matches[0], $udata.$btns, $contents);
}

function get_com_inherited($class_id, &$inh_com, &$count){
  $i_max = getMultData("SELECT * FROM compatible WHERE class1_id=".$class_id, $data);
  for ($i = 0; $i < $i_max; $i++){
    $inh_com[$count]["id"] = $data[$i]["class2_id"];
    $inh_com[$count]["class_id"] = $data[$i]["class1_id"];
    $count++;
  };

  $i_max = getMultData("SELECT * FROM compatible WHERE class2_id=".$class_id, $data);
  for ($i = 0; $i < $i_max; $i++){
    $inh_com[$count]["id"] = $data[$i]["class1_id"];
    $inh_com[$count]["class_id"] = $data[$i]["class2_id"];
    $count++;
  };

  getData("SELECT parent_id FROM class WHERE id=".$class_id, $parentdata);
  getData("SELECT * FROM class c, term t WHERE t.id=c.term_id and c.id=".$parentdata["parent_id"], $parentdata1);
  if ($parentdata1["text"] != "Thing")
    get_com_inherited($parentdata["parent_id"], $inh_com, $count);
};

function com_child($class_id, &$child_com, &$count){
  $i_max = getMultData("SELECT * FROM compatible WHERE class1_id=".$class_id, $data);
  for ($i = 0; $i < $i_max; $i++){
    $child_com[$count]["id"] = $data[$i]["class2_id"];
    $child_com[$count]["class_id"] = $data[$i]["class1_id"];
    $count++;
  };

  $i_max = getMultData("SELECT * FROM compatible WHERE class2_id=".$class_id, $data);
  for ($i = 0; $i < $i_max; $i++){
    $child_com[$count]["id"] = $data[$i]["class1_id"];
    $child_com[$count]["class_id"] = $data[$i]["class2_id"];
    $count++;
  };

  $i_max = getMultData("SELECT id FROM class WHERE parent_id=".$class_id, $childdata);
  for ($i = 0; $i < $i_max; $i++)
    com_child($childdata[$i]["id"], $child_com, $count);
};

function createCompatible() {
  global $_GET, $data, $path, $style, $session_id, $language, $header;

  if (strrpos(substr($_GET["uid"], 3), "com")==false)
    $_GET["uid"] = substr($_GET["uid"], 3);
  else
    $_GET["uid"] = substr($_GET["uid"], 3, strrpos($_GET["uid"], "com")-3);

  if ($data["type"] >= 1) { //user
    $filename = $path[$style]["tpl"]."/action.xml";
    $fp = fopen($filename, "r");
    $contents = fread($fp, filesize ($filename));
    fclose ($fp);
    preg_match("/\%REPEAT\%(.*?)\%REPEAT\%/s", $contents, $matches);

    $sql_tmp = "SELECT type_id FROM ontology WHERE id = (SELECT onto_id FROM class WHERE id = ".$_GET["uid"].")";
    $ontotype = Array();
    getData($sql_tmp, $ontotype);
    if (!($ontotype["type_id"] == "5")){
      if (getdata("SELECT * FROM compatible WHERE (class1_id=".$_GET["uid"]." and class2_id=-1)or(class2_id=".$_GET["uid"]." and class1_id=-1)", $data) != 1){

        if (getData("SELECT onto_id FROM class where id = ".$_GET["uid"].";", $odatatmp) != 1) return "";
        insertRecord("INSERT INTO compatible (onto_id, class1_id, class2_id, session_id) VALUES(".
          $odatatmp["onto_id"].", ".$_GET["uid"].", -1, '".
          $session_id."');");

        if (getData("SELECT id FROM compatible where session_id = '".
          $session_id."';", $datatmp) == 1) {
          $sql_str = "UPDATE compatible SET session_id = '' WHERE id=".
          $datatmp["id"].";";
          insertRecord($sql_str);
          $contents = str_replace("%SCCSS%", "1", $contents);
          $childs[0]["id"] = -1;

      } else {
        $childs[0]["id"] = "0";
        $contents = str_replace("%SCCSS%", "0", $contents);
        $datatmp["id"] = 0;
      }
    }else{
        $childs[0]["id"] = "0";
        $contents = str_replace("%SCCSS%", "0", $contents);
        $datatmp["id"] = 0;
    };
  }else{
    $childs[0]["id"] = "0";
    $contents = str_replace("%SCCSS%", "0", $contents);
    $datatmp["id"] = 0;
  }
  } else {
    $childs[0]["id"] = "0";
    $contents = str_replace("%SCCSS%", "0", $contents);
    $datatmp["id"] = 0;
  }
  $tmp_cont = str_replace("%NODE_ID%", "com".$_GET["uid"]."com".$childs[0]["id"], $matches[1]);
  $tmp_cont = str_replace("%NODE_NM%", getMessage("Name"), $tmp_cont);
  $tmp_cont = str_replace("%IMG_TYPE%", "class", $tmp_cont);
  $tmp_cont = str_replace("%NODE_LN%", "index.php?trg=ontoedit&amp;act=compatibledata&amp;sid=".
    $session_id."&amp;uid=com".$childs[0]["id"], $tmp_cont);
  $contents = str_replace($matches[0], $tmp_cont, $contents);
  $contents = str_replace("%XMLSRC%", "index.php?trg=ontoedit&amp;act=compatiblelist&amp;sid=".
    $session_id."&amp;uid=com".$_GET["uid"]."&amp;sel=com".$_GET["uid"]."com".$childs[0]["id"], $contents);
  $contents = str_replace("%ACT_ID%", "Add", $contents);
  $contents = str_replace("%PARENT%", "com".$_GET["uid"], $contents);
  header ($header["xml"]);
  return $contents;
}

function deleteCompatible($ID) {
  global $data, $path, $style, $session_id, $header;

  $filename = $path[$style]["tpl"]."/action.xml";
  $fp = fopen($filename, "r");
  $contents = fread($fp, filesize ($filename));
  fclose ($fp);

  preg_match("/\%REPEAT\%(.*?)\%REPEAT\%/s", $contents, $matches);

  $sql_tmp = "SELECT type_id FROM ontology WHERE id = (SELECT onto_id FROM class WHERE id = ".$ID[0].")";
  $ontotype = Array();
  getData($sql_tmp, $ontotype);
  if (!($ontotype["type_id"] == "5")){
    if (isset($ID[1])){
      $count = 0; $inh_com = Array();
      getData("SELECT parent_id FROM class WHERE id=".$ID[0], $parentdata);
      getData("SELECT * FROM class c, term t WHERE t.id=c.term_id and c.id=".$parentdata["parent_id"], $parentdata1);
      if ($parentdata1["text"] != "Thing")
        get_com_inherited($parentdata["parent_id"], $inh_com, $count);
      $b = true;
      for ($i = 0; $i < count($inh_com); $i++)
        if ($inh_com[$i] == $ID[1])
          $b = false;
    };


    if (($data["type"] >= 1 && isset($ID[1]))&&($b)){
        insertRecord("DELETE FROM compatible WHERE (class1_id=".$ID[1]." and class2_id=".$ID[0].") or (class1_id=".$ID[0]." and class2_id=".$ID[1].");");
        $contents = str_replace("%SCCSS%", "1", $contents);
    }
    else {
      $contents = str_replace("%SCCSS%", "0", $contents);
      $ID[1] = 0;
    }
  }else{
    $contents = str_replace("%SCCSS%", "0", $contents);
    $ID[1] = 0;
  };
  $contents = str_replace($matches[0], "", $contents);
  $contents = str_replace("%ACT_ID%", "Delete", $contents);
  $contents = str_replace("%PARENT%", "com".$ID[0]."com".$ID[1], $contents);
  $contents = str_replace("%SELECT%", "com".$ID[0], $contents);
  header ($header["xml"]);
  return $contents;
}

function updateCompatible() {
  global $_GET, $data, $path, $style, $session_id, $term_Thing_ID, $language, $header;
  $comment = "";
  if ($data["type"] >= 1) {
    $_GET["ohidd"] = utf8Encode($_GET["ohidd"]);
    $_GET["ohidd2"] = utf8Encode($_GET["ohidd2"]);
    if (isset($_GET["otype"]))
      $_GET["otype"] = utf8Encode($_GET["otype"]);
    $_GET["otype1"] = utf8Encode($_GET["otype1"]);

    if (isset($_GET["otype"])){
/*      $childs_com = Array(); $count = 0;
      com_child($_GET["ohidd"], $childs_com, $count);

      for ($i = 0; $i < count($childs_com); $i++)
        if ($_GET["otype"] == $childs_com[$i]["id"])
          insertRecord("DELETE FROM compatible WHERE (class1_id=".$_GET["otype"]." and class2_id=".$childs_com[$i]["class_id"].") or (class2_id=".$_GET["otype"]." and class1_id=".$childs_com[$i]["class_id"].")");
*/
      $sql_str = "UPDATE compatible SET class2_id = ".$_GET["otype"].", is_compatible=".$_GET["otype1"].
        " WHERE class1_id=".$_GET["ohidd"]." and class2_id=".$_GET["ohidd2"];
      insertRecord($sql_str);

      $sql_str = "UPDATE compatible SET class1_id = ".$_GET["otype"].", is_compatible=".$_GET["otype1"].
        " WHERE class2_id=".$_GET["ohidd"]." and class1_id=".$_GET["ohidd2"];
      insertRecord($sql_str);
      $class_id = $_GET["otype"];
    }else{
      $class_id = $_GET["ohidd"];
      $sql_str = "UPDATE compatible SET is_compatible=".$_GET["otype1"].
        " WHERE class1_id=".$_GET["ohidd"]." and class2_id=".$_GET["ohidd2"];
      insertRecord($sql_str);

      $sql_str = "UPDATE compatible SET is_compatible=".$_GET["otype1"].
        " WHERE class2_id=".$_GET["ohidd"]." and class1_id=".$_GET["ohidd2"];
      insertRecord($sql_str);
      $class_id = -1;
    };
  }
  header ($header["xml"]);
  $contents = compatibleData($class_id);
  return $contents;
}

?>